using ASP;
using System;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class ListPager : UserControl
{
	internal sealed class PagingItem
	{
		private int _pageValue;
		private int _pageText;
		public int PageValue
		{
			get
			{
				return this._pageValue;
			}
			set
			{
				this._pageValue = value;
			}
		}
		public int PageText
		{
			get
			{
				return this._pageText;
			}
			set
			{
				this._pageText = value;
			}
		}
	}
	public delegate void PagingItemHandler(object sender, EventArgs e);
	protected int _pageIndex;
	protected int _rowsSize;
	protected int _totalCount;
	protected int _pageSize;
	protected int _pageCount;
	protected bool _isLink;
	protected string _hyperLink;
	protected string _selectClass;
	protected HiddenField hfPageSize;
	protected HiddenField hfPageIndex;
	protected HiddenField hfTotalCount;
	protected LinkButton lbPrev;
	protected Repeater rpView;
	protected LinkButton lbNext;
	private event ListPager.PagingItemHandler _PagingItemClick;
	public event ListPager.PagingItemHandler PagingItemClick
	{
		add
		{
			this._PagingItemClick += value;
		}
		remove
		{
			this._PagingItemClick -= value;
		}
	}
	public bool ShowLink
	{
		set
		{
			this._isLink = value;
		}
	}
	public int RowSize
	{
		set
		{
			this._rowsSize = value;
		}
	}
	public int PageIndex
	{
		get
		{
			return this._pageIndex;
		}
		set
		{
			this._pageIndex = value;
		}
	}
	public string HyperLink
	{
		set
		{
			this._hyperLink = value;
		}
	}
	public int PageCount
	{
		get
		{
			return this._pageCount;
		}
	}
	public string SelectClass
	{
		set
		{
			this._selectClass = value;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public ListPager()
	{
		this._isLink = false;
		this._hyperLink = string.Empty;
		this._pageIndex = 0;
		this._rowsSize = 6;
		this._totalCount = 0;
		this._pageSize = 10;
		this._pageCount = 0;
		this._PagingItemClick = null;
		this._selectClass = "active";
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (base.IsPostBack)
		{
			this.SetData(Convert.ToInt32(this.hfPageIndex.Value), Convert.ToInt32(this.hfPageSize.Value), Convert.ToInt32(this.hfTotalCount.Value));
		}
	}
	protected void OnPage_Select(object sender, EventArgs e)
	{
		LinkButton linkButton = (LinkButton)sender;
		if (linkButton.ID.Equals("lbPrev"))
		{
			this._pageIndex = Convert.ToInt32(this.hfPageIndex.Value) - 1;
			if (this._pageIndex <= 0)
			{
				this._pageIndex = 0;
				this.hfPageIndex.Value = "0";
			}
		}
		else
		{
			if (linkButton.ID.Equals("lbNext"))
			{
				this._pageIndex = Convert.ToInt32(this.hfPageIndex.Value) + 1;
			}
			else
			{
				this._pageIndex = Convert.ToInt32(linkButton.CommandArgument);
			}
		}
		if (this._PagingItemClick != null)
		{
			this._PagingItemClick(sender, null);
		}
	}
	public void SetData(int _pageIndex, int _pageSize, int _totalCount)
	{
		this._pageIndex = _pageIndex;
		this._pageSize = _pageSize;
		this._totalCount = _totalCount;
		if (_pageIndex < 0)
		{
			_pageIndex = 0;
		}
		if (_pageIndex > 0)
		{
			this.hfPageIndex.Value = _pageIndex.ToString();
		}
		this.hfTotalCount.Value = _totalCount.ToString();
		this.hfPageSize.Value = _pageSize.ToString();
		if (_totalCount > 0)
		{
			this._pageCount = _totalCount / _pageSize + ((_totalCount % _pageSize == 0) ? 0 : 1);
		}
		else
		{
			this._pageCount = 0;
		}
		if (this._pageCount < 1)
		{
			this.rpView.DataSource = null;
			this.rpView.DataBind();
		}
		else
		{
			int num = 0;
			int num2 = (this._rowsSize > 0) ? this._rowsSize : 10;
			while (_pageIndex < num || _pageIndex >= num2)
			{
				num += this._rowsSize;
				num2 += this._rowsSize;
			}
			if (num2 > this._pageCount)
			{
				num2 = this._pageCount;
			}
			ListPager.PagingItem[] array = new ListPager.PagingItem[num2 - num];
			int num3 = array.Length;
			for (int i = 0; i < num3; i++)
			{
				array[i] = new ListPager.PagingItem();
				array[i].PageValue = num + i;
				array[i].PageText = num + i + 1;
			}
			this.rpView.DataSource = array;
			this.rpView.DataBind();
		}
	}
	public void SetData(int _pageIndex, int _totalCount)
	{
		this.SetData(_pageIndex, Convert.ToInt32(this.hfPageSize.Value), _totalCount);
	}
	protected void rpView_ItemCreated(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item != null)
		{
			ListPager.PagingItem pagingItem = (ListPager.PagingItem)e.Item.DataItem;
			if (pagingItem != null)
			{
				Control control;
				if (this._isLink)
				{
					control = e.Item.FindControl("hlLink");
					((HyperLink)control).Enabled = (this._pageIndex != pagingItem.PageValue);
				}
				else
				{
					control = e.Item.FindControl("lbLink");
					((LinkButton)control).Enabled = (this._pageIndex != pagingItem.PageValue);
				}
				if (this._pageIndex == pagingItem.PageValue)
				{
					((HtmlGenericControl)e.Item.FindControl("liLink")).Attributes["class"] = this._selectClass;
				}
				control.Visible = true;
			}
		}
	}
}
